<template>
  <div class="goods-list">
    <h3 class="title">商品列表</h3>
    <el-table :data="list" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="data">
          <div class="goodstext">
            <span>商品名称：{{ data.row.name }}</span>
            <span>商品分类：{{ data.row.category }}</span>
          </div>
          <div class="goodstext">
            <span>创建时间：{{ data.row.ctime }}</span>
            <span>商品ID：{{ data.row.id }}</span>
          </div>
          <div class="goodstext">
            <span>商品价格：{{ data.row.price.toFixed(2) }}</span>
            <span>商品评价：{{ data.row.rating }}</span>
          </div>
          <div class="goodstext">
            <span>商品描述：{{ data.row.goodsDesc }}</span>
            <span>商品销量：{{ data.row.sellCount }}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="商品名称" prop="name"> </el-table-column>
      <el-table-column label="所属分类" prop="category"> </el-table-column>
      <el-table-column label="商品价格" prop="price"> </el-table-column>
      <el-table-column label="商品图片" prop="desc">
        <template slot-scope="obj">
          <img width="80px" :src="obj.row.imgUrl" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="商品描述" prop="goodsDesc"> </el-table-column>
      <el-table-column width="200" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" size="mini">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" size="mini" type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-sizes="pagesizes"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="修改商品" :visible.sync="dialog" center>
      <div class="changtext">
        <div>商品名称：</div>
        <div class="changeipt"><el-input v-model="change.name" /></div>
      </div>
      <div class="changtext">
        <div>商品分类：</div>
        <div>
          <el-select v-model="change.category" placeholder="请选择">
            <el-option
              v-for="item in categories"
              :key="item.cateName"
              :value="item.cateName"
            ></el-option>
          </el-select>
        </div>
      </div>
      <div class="changtext">
        <div>商品价格：</div>
        <div class="changeipt"><el-input v-model="change.price" /></div>
      </div>
      <div class="changtext">
        <div>商品图片：</div>
        <div class="changeipt">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </div>
      <div class="changtext">
        <div>商品描述：</div>
        <div class="changeipt">
          <el-input v-model="change.goodsDesc" type="textarea" />
        </div>
      </div>
      <div>
        <el-button @click="dialog = false">取消</el-button>
        <el-button type="primary" @click="clickChange">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getChinatime } from "@/utils/utils";
import {
  $_goodslist,
  $_delgoods,
  $_goodscategories,
  $_changegoods,
} from "@/apis/goods";
export default {
  data() {
    return {
      list: [], //初始渲染的数据数组
      curpage: 1, //初始第几页
      pagesize: 5, //默认每页数据的条数
      pagesizes: [5, 10, 15, 20], //每页数据条数数组
      total: 0, //总条数
      dialog: false,
      change: [], //编辑接收的数据数组
      categories: [], //获取分类列表
      imageUrl: "", //回显的图片src
      newimage: "", //上传成功后新的图片名
    };
  },
  created() {
    this.reqTable();
  },
  methods: {
    reqTable() {
      $_goodslist({ currentPage: this.curpage, pageSize: this.pagesize }).then(
        (res) => {
          for (let obj of res.data.data) {
            obj.ctime = getChinatime(obj.ctime);
            obj.price = obj.price.toFixed(2);
          }
          this.list = res.data.data;
          this.total = res.data.total;
        }
      );
    },
    handleEdit(row) {
      this.dialog = true;
      this.change = row;
      this.imageUrl = row.imgUrl;
      this.newimage = row.imageUrl;
      console.log(row);
      $_goodscategories().then((res) => {
        this.categories = res.data.categories;
      });
    },
    clickChange() {
      $_changegoods({
        name: this.change.name,
        category: this.change.category,
        price: this.change.price,
        imgUrl:
          this.newimage ||
          this.imageUrl.substring(this.imageUrl.lastIndexOf("/") + 1),
        goodsDesc: this.change.goodsDesc,
        id: this.change.id,
      }).then((res) => {
        this.dialog = false;
        console.log(res);
        if (res.data.code == 0) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.reqTable();
        }
      });
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该分类, 是否继续?", "危险操作慎重选择", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        $_delgoods({ id: row.id }).then((res) => {
          if (res.data.code == 0) {
            this.$message({
              message: res.data.msg,
              type: "success",
            });
            this.reqTable();
          }
        });
      });
    },
    handleSizeChange(newsize) {
      this.pagesize = newsize;
    },
    handleCurrentChange(newpage) {
      this.curpage = newpage;
      this.reqTable();
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.newimage = res.imgUrl;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      let types = ["image/jpeg", "image/png", "image/webp"];
      const isJPG = types.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG、png、webp 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang='less' scoped>
.goods-list {
  width: 98%;
  height: 100%;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 20px #212121;
  .title {
    padding-top: 10px;
  }
  .goodstext {
    display: flex;
    justify-content: space-between;
    padding: 10px;
  }
  .changtext {
    display: flex;
    align-items: center;
    padding: 10px;
    .changeipt {
      width: 240px;
    }
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}
</style>